const app = getApp()
import util from '../../../utils/util'
import {
  MemberUpEventList,
  GetGriderEventList,
  GetWsxfList
} from '../../../config/api'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    adminInfo:'', // 管理者
    cur_index: 0, // 高亮选项
    type: '', // 下级处理type
    isEmpty: false, // 是否为空
    isBottom:false, // 是否无数据触底了
    isReachBtm:false, // 是否触发上拉加载

    grider: '', // 网格员信息
    grider_id: '',
   // cat_id: 0, // 事件类型
    page: 1,
    limit: 10,
    lists: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options)
    let adminInfo = wx.getStorageSync('adminInfo');
    let grider = wx.getStorageSync('grider');

    this.setData({
      adminInfo,
      grider: grider,
      //cat_id: options.cat_id
    })

    if (options.grider_id) {
      console.log('下级处理进入')
      this.setData({
        type: options.type,
        grider_id: options.grider_id
      })
    } else {
      console.log('正常进入')
      let grider = wx.getStorageSync('grider');
      this.setData({
        grider_id: grider.id
      })
    }

    if (adminInfo) { // 判断管理员
      this.getAdminCunmingList()
    } else {
      this.getList()
    }
  },

  // switchTab(e) {
  //   let cat_id = e.currentTarget.dataset.cat_id;
  //   this.setData({
  //     cur_index: cat_id,
  //     cat_id,
  //     lists: [],
  //     page: 1
  //   })
  //   this.getList();
  // },

  getAdminCunmingList() { // 获取管理员村民上报列表
    let that = this
    let params = {
      vollege_id: app.globalData.vollege_id,
      page: that.data.page,
      limit: that.data.limit
    }
    wx.showLoading({
      title: '玩命加载中',
    })
    util.apiRequest(MemberUpEventList, params, null).then(r => {
      console.log('管理员村民上报：', r)
      var newList = that.data.lists.concat(r.data.data)

      if (r.data.data == '' && that.data.isReachBtm==true) {
        that.setData({
          isBottom: true
        })
        wx.showToast({
          title: '暂无更多',
          icon: 'none',
        })
      }

      that.setData({
        lists: newList
      })

      wx.hideLoading();

      if (newList.length == 0) {
        that.setData({
          isEmpty: true
        })
      } else {
        that.setData({
          isEmpty: false
        })
      }
    })
  },

  getList() { // 获取列表
    let that = this;
    let params = {
      id: this.data.grider_id,
      vollege_id: this.data.grider.vollege_id,
      page: this.data.page,
      limit: this.data.limit
    }
    util.apiRequest(GetGriderEventList, params, null).then(r => {
      console.log('2事件列表', r)
      var newList = that.data.lists.concat(r.data.data)
      if (r.data.data.length == 0 && that.data.isReachBtm == true) {
        that.setData({
          lists: newList,
          isBottom: true
        })
        wx.showToast({
          title: '暂无更多',
          icon: 'none',
        })
      } else {
        that.setData({
          lists: newList
        })
      }
      wx.hideLoading();

      if (newList.length == 0) {
        that.setData({
          isEmpty: true
        })
      } else {
        that.setData({
          isEmpty: false
        })
      }

    })
  },

  checkDetail(e) {
    let id = e.currentTarget.dataset.id;
    let cat_id = e.currentTarget.dataset.cat_id;
    //let status = e.currentTarget.dataset.status;
    wx.navigateTo({
      url: '/pages/grid/unresolvedDetail/unresolvedDetail?id=' + id + '&type=' + this.data.type
    })
    // if (cat_id == 1) {
    //   wx.navigateTo({
    //     url: '/pages/grid/grider_feedback_detail/grider_feedback_detail?id=' + id,
    //   })
    // }else if(this.data.adminInfo){
    //   wx.navigateTo({
    //     url: '/pages/grid/adminEventDetail/adminEventDetail?id=' + id
    //   })
    // }else {
    //   wx.navigateTo({
    //     url: '/pages/grid/unresolvedDetail/unresolvedDetail?id=' + id + '&type=' + this.data.type
    //   })
    // }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    var that = this;
    var page = this.data.page;

    that.setData({
      isReachBtm:true
    })
    if(that.data.isBottom) return;

    page++;
    that.setData({
      page
    })
    console.log('page++', page)
    
    if (this.data.adminInfo) {
      this.getAdminCunmingList()
    } else {
      this.getList()
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})